package com.sctvcloud.zhaohua_sd.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.githang.statusbar.StatusBarCompat;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.zhaohua_sd.R;
import com.sctvcloud.zhaohua_sd.beans.ServiceBean;
import com.sctvcloud.zhaohua_sd.beans.ServiceDataBean;
import com.sctvcloud.zhaohua_sd.ui.adapter.HighRiseAdapter;
import com.sctvcloud.zhaohua_sd.ui.adapter.ServiceRecentlyUsedServiceAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 服务模块的不同类型的服务展示
 */
public class ServiceColumnFragment extends ColumnBaseFragment implements ServiceRecentlyUsedServiceAdapter.OnServiceItemClickListener {

    @BindView(R.id.service_list)
    protected RecyclerView recyclerView;

    private ServiceRecentlyUsedServiceAdapter adapter;
    private List<ServiceDataBean> serviceList;
    private OnServiceUsedListener serviceUsedListener;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_service_pager, container, false);
        ButterKnife.bind(this, view);
        initRecentlyView();
        return view;
    }

    private void initRecentlyView() {
        GridLayoutManager mgr = new GridLayoutManager(getContext(), 4);
        recyclerView.setLayoutManager(mgr);
//        recentlyList.setItemAnimator(new DefaultItemAnimator());
        adapter = new ServiceRecentlyUsedServiceAdapter();
        adapter.setServiceItemClickListener(this);
        recyclerView.setAdapter(adapter);
        adapter.setData(serviceList);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        isCoverStatusColor = false;
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }


    public void setServiceList(List<ServiceDataBean> list) {
        if(ListUtils.isListValued(list)) {
            if (null != serviceList) {
                serviceList.clear();
            } else {
                serviceList = new ArrayList<>();
            }
            this.serviceList.addAll(list);
            if (null != adapter) {
                adapter.setData(serviceList);
            }
        }
    }

    @Override
    public void onServiceItemClick(ServiceDataBean item) {
        if(null!=serviceUsedListener){
            serviceUsedListener.onUsedService(item);
        }
    }

    public void setServiceUsedListener(OnServiceUsedListener serviceUsedListener) {
        this.serviceUsedListener = serviceUsedListener;
    }

    public interface OnServiceUsedListener{

        void onUsedService(ServiceDataBean item);
    }
}
